package com.example.buildtable.controller;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaJoinQueryWrapper;
import com.example.buildtable.pojo.City;
import com.example.buildtable.pojo.MemberShiplevel;
import com.example.buildtable.pojo.User;
import com.example.common.util.R;
import com.example.buildtable.pojo.Member;
import com.example.buildtable.service.MemberService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@Tag(name = "会员")
@RequestMapping(value = "/member/")
public class MemberController {
    @Autowired
    private MemberService memberService;
    
    @Operation(summary = "查询所有会员数据")
    @GetMapping(value = "/getAllMember")
    public R getAllMember() {
        LambdaJoinQueryWrapper<Member> wrapper = new LambdaJoinQueryWrapper<>(Member.class);
        wrapper.innerJoin(City.class, City::getCityId, Member::getMemberCityId).
                innerJoin(MemberShiplevel.class, MemberShiplevel::getMemberShiplevelId, Member::getMemberShiplevelId);
        List<Member> memberList = memberService.list(wrapper);
        return R.ok("查询所有会员数据成功").data("memberList", memberList);
    }

    @Operation(summary = "登录会员")
    @PostMapping(value = "/login")
    public R login(Member member) {
        Map<String, Object> map = memberService.login(member);
        if (map.get("state").equals("success")) {
            return R.ok("会员登录成功").data("member", member);
        } else {
            return R.error("会员登录失败").data(map);
        }
    }

    @Operation(summary = "注册会员")
    @PostMapping(value = "/addMember")
    public R addMember(Member member) {
        boolean flag = memberService.save(member);
        if (flag) {
            return R.ok("会员注册成功");
        } else {
            return R.ok("会员注册失败");
        }
    }

    @Operation(summary = "退出登录会员")
    @PostMapping(value = "/loginOut")
    public R loginOut(HttpServletRequest request) {
        //清理session
        request.getSession().removeAttribute("member");
        if (request.getAttribute("member") == null) {
            return R.ok("会员退出登录成功");
        } else {
            return R.error("会员退出登录失败");
        }
    }

    @Operation(summary = "注销会员")
    @GetMapping(value = "/deleteMember/{memberId}")
    public R deleteMember(@PathVariable("memberId") Integer memberId) {
        boolean flag = memberService.removeById(memberId);
        if (flag) {
            return R.ok("会员注销成功");
        } else {
            return R.ok("会员注销失败");
        }
    }

    @Operation(summary = "按照会员查看")
    @GetMapping(value = "/getMemberById/{memberId}")
    public R getMemberById(@PathVariable("memberId") Integer memberId) {
        LambdaJoinQueryWrapper<Member> wrapper = new LambdaJoinQueryWrapper<>(Member.class);
        wrapper.innerJoin(City.class, City::getCityId, Member::getMemberCityId).
                innerJoin(MemberShiplevel.class, MemberShiplevel::getMemberShiplevelId, Member::getMemberShiplevelId);
        wrapper.eq(Member::getMemberId, memberId);
        Member member = memberService.getOne(wrapper);
        if (member != null) {
            return R.ok("按照会员查看成功").data("member", member);
        } else {
            return R.error("按照会员查看失败");
        }
    }

    @Operation(summary = "修改会员")
    @PostMapping(value = "/updateMember")
    public R updateMember(Member member) {
        boolean flag = memberService.updateById(member);
        if (flag) {
            return R.ok("会员修改成功");
        } else {
            return R.ok("会员修改失败");
        }
    }
}